﻿using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RedisDemo
{
    public partial class MainForm : Form
    {
        readonly RedisUtil util = new RedisUtil();

        public MainForm()
        {
            InitializeComponent();

            try
            {
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message+"\n"+exception.StackTrace, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            comboBox_Type.SelectedIndex = 0;
        }

        private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            try
            {
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message + "\n" + exception.StackTrace, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Button_Start_Click(object sender, EventArgs e)
        {
            try
            {
                if (util.Start())
                {
                    toolStripStatusLabel.Text = "redis服务端开启成功";
                }
                else
                {
                    toolStripStatusLabel.Text = "redis服务端开启失败";
                }
            }
            catch (Exception exception)
            {
                toolStripStatusLabel.Text = "redis服务端开启失败";
                MessageBox.Show(exception.Message + "\n" + exception.StackTrace, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Button_Close_Click(object sender, EventArgs e)
        {
            try
            {
                if (util.Close())
                {
                    toolStripStatusLabel.Text = "redis服务端关闭成功";
                }
                else
                {
                    toolStripStatusLabel.Text = "redis服务端关闭失败";
                }
            }
            catch (Exception exception)
            {
                toolStripStatusLabel.Text = "redis服务端关闭失败";
                MessageBox.Show(exception.Message + "\n" + exception.StackTrace, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Button_Connect_Click(object sender, EventArgs e)
        {
            try
            {
                util.Connect();
                toolStripStatusLabel.Text = "redis服务端连接成功";
            }
            catch (Exception exception)
            {
                toolStripStatusLabel.Text = "redis服务端连接失败";
                MessageBox.Show(exception.Message + "\n" + exception.StackTrace, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Button_Disconnect_Click(object sender, EventArgs e)
        {
            try
            {
                util.Disconnect();
                toolStripStatusLabel.Text = "连接断开成功";
            }
            catch (Exception exception)
            {
                toolStripStatusLabel.Text = "连接断开失败";
                MessageBox.Show(exception.Message + "\n" + exception.StackTrace, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Button_AllKeyValue_Click(object sender, EventArgs e)
        {
            try
            {
                List<string> result = util.AllKeyValue();
                richTextBox_AllKeyValue.Clear();
                richTextBox_AllKeyValue.AppendText(string.Join("\r\n", result));
                toolStripStatusLabel.Text = "查询所有键值对成功";
            }
            catch (Exception exception)
            {
                toolStripStatusLabel.Text = "查询所有键值对失败";
                MessageBox.Show(exception.Message + "\n" + exception.StackTrace, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Button_Add_Click(object sender, EventArgs e)
        {
            try
            {
                string type = comboBox_Type.Text.Trim();
                string id = textBox_Key.Text.Trim();
                string value = "";
                switch (type)
                {
                    case "string":
                        value = richTextBox_Value.Text.Trim();
                        StringItem stringItem = new StringItem(id, value);
                        util.Add(stringItem);
                        break;
                    case "hash":
                        string[] ps = richTextBox_Value.Text.Trim().Split(' ');
                        Dictionary<string, string> pairs = new Dictionary<string, string>();
                        for (int i = 0; i < ps.Length; i += 2)
                        {
                            pairs.Add(ps[i], ps[i + 1]);
                        }
                        HashItem hashItem = new HashItem(id, pairs);
                        util.Add(hashItem);
                        break;
                    case "list":
                        value = richTextBox_Value.Text.Trim();
                        ListItem listItem = new ListItem(id, value);
                        util.Add(listItem);
                        break;
                    case "set":
                        value = richTextBox_Value.Text.Trim();
                        SetItem setItem = new SetItem(id, value);
                        util.Add(setItem);
                        break;
                    case "zset":
                        value = richTextBox_Value.Text.Trim();
                        ZSetItem zSetItem = new ZSetItem(id, 0, value);
                        util.Add(zSetItem);
                        break;
                }
                toolStripStatusLabel.Text = "添加键值对成功";
            }
            catch (Exception exception)
            {
                toolStripStatusLabel.Text = "添加键值对失败";
                MessageBox.Show(exception.Message + "\n" + exception.StackTrace, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Button_Search_Click(object sender, EventArgs e)
        {
            try
            {
                string key = textBox_Key.Text.Trim();
                string value = util.Search(key);
                richTextBox_AllKeyValue.Text = value;
                toolStripStatusLabel.Text = "查询键值对成功";
            }
            catch (Exception exception)
            {
                toolStripStatusLabel.Text = "查询键值对失败";
                MessageBox.Show(exception.Message + "\n" + exception.StackTrace, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void Button_Delete_Click(object sender, EventArgs e)
        {
            try
            {
                string key = textBox_Key.Text.Trim();
                util.Delete(key);
                toolStripStatusLabel.Text = "删除键值对成功";
            }
            catch (Exception exception)
            {
                toolStripStatusLabel.Text = "删除键值对失败";
                MessageBox.Show(exception.Message + "\n" + exception.StackTrace, "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
